import pandas as pd
import random

rng = list(pd.date_range('1/1/2017', periods=24, freq='M'))  # 生成日期
mony = [random.randint(1000, 9000) for i in range(0, 24)]  # 随机生成销售额
data = pd.DataFrame({'rng': rng, 'mony': mony})  # 组成一个dataframe

print(data)
print("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★1")
# 计算环比增长(一阶差分函数)
data['环比_1'] = data.mony.diff()
print(data)
print("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★2")
data['环比_2'] = data.mony.pct_change()
data.fillna(0, inplace=True)

print(data)

print("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★3")
data['tongbi_shu'] = data.mony.diff(12)
data.fillna(0, inplace=True)
data['同比增长'] = data['tongbi_shu'] / (data['mony'] - data['tongbi_shu'])
print(data)
